﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess.Classes;

public partial class Admin_EditCategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            PopulateControls();
    }
    private void PopulateControls()
    {
        string cid = Request.QueryString["cid"] ?? "";
        // Neu co QueryString cid ==> cap nhat ThuongHieu
            if (cid != "")
            {
                // Lay category theo gia tri cid
                ThuongHieu data = ThuongHieu.Single(cid);
                // Khong ton tai thuong hieu nay trong he thong
                // chuyen huong ve trang Category.aspx
                if (data == null)
                    Response.Redirect("~/Admin/Category.aspx");

                // Nguoc lai thi dua du lieu vao form de tien hanh cap nhap
                // Dat ten trang web
                lblTitle.Text = "Edit Brand";

                // Luu lai gia tri ID cua thuong hieu hien dang cap nhat
                lblId.Text = data.MaThuongHieu.ToString();

                // Gan cac thong tin con lai vao form
                txtName.Text = data.TenThuongHieu;
                txtMa.Text = data.MaThuongHieu;
                txtMa.Enabled = false;
                btnReset.Visible = false;
            }
            else
            {
                // Khong co QueryString cid ==> them moi thuong hieu
                // Dat ten trang web
                lblTitle.Text = "Add Brand";

                // Hien nut reset (xoa trang form de nhap lai)
                btnReset.Visible = true;
            }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        // Them su kien cho nut Save
        btnSave.Click += new EventHandler(btnSave_Click);
        //btnReset.Click += new EventHandler(btnReset_Click);
    }
    // Lay du lieu tu form de them moi/cap nhat
    private ThuongHieu GetData()
    {
        ThuongHieu data = null;
        if (lblTitle.Text != "Thêm thương hiệu")
            // lay thong tin cu tu Database de cap nhat
            data = ThuongHieu.Single(lblId.Text);
        else
        {
            data = new ThuongHieu(); // them moi
            data.MaThuongHieu = txtMa.Text;
        }

        // lay Name tu textbox Name
        //data = new ThuongHieu();
        data.TenThuongHieu = txtName.Text;
        
        return data;
    }

    void btnSave_Click(object sender, EventArgs e)
    {
        // Neu tat ca du lieu deu duoc nhap hop le
        if (Page.IsValid)
        {
            bool rs = false;
            // Lay du lieu tu form
            ThuongHieu data = GetData();
            
            // ID > 0 ==> Cap nhat va hien thong bao
            if (lblTitle.Text != "Thêm thương hiệu")
            {
                rs = ThuongHieu.Update(data);
                lblStatus.Text = rs ? "Chỉnh sửa thành công" : "Lỗi";
            }
            else
            {
                rs = ThuongHieu.Add(data);
                lblStatus.Text = rs ? "Thêm thành công" : "Lỗi";
                // Neu them thanh cong thi xoa trang form de them tiep
                if (rs)
                    ResetForm();        
            }
        }
    }
    // Xoa trang form
    private void ResetForm()
    {
        txtName.Text = "";
        txtMa.Text = "";
    }
}